﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormPlanMes
    Public usr As String
    Public ok_num As String
    Public b_gt_sys_id As String
    Public material_code As String
    Dim objFlbMst As New clsFlbMst
    Dim objGjbMst As New clsGjbMst
    Dim objWorkshopCurrent As New clsWorkshopCurrent
    Dim objPgdMesGt As New clsPgdMesGt
    Dim objGtPgdMesWorkReport As New clsGtPgdMesWorkReport
    Dim httpHelper As New HttpHelper
    Dim txt As TextBox
    Private Sub FormPlanMes_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim startDate As String = Date.Now.ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        setDgvData()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        setDgvData()
    End Sub

    Private Sub setDgvData()
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            Dim dt As DataTable = objPgdMesGt.selectList(txtStartDate.Text, txtEndDate.Text, material_code)
            If dt.Rows.Count > 0 Then
                dt.Columns.Add("oper_type_name")
                dt.Columns.Add("validity_name")
                For Each row As DataRow In dt.Rows
                    If row("oper_type") = "0" Then
                        row("oper_type_name") = "骨架板"
                    Else
                        row("oper_type_name") = "法兰板"
                    End If
                    If row("validity") = "0" Then
                        row("validity_name") = "无效"
                    Else
                        row("validity_name") = "有效"
                    End If
                Next
            End If
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dt
        Else
            MsgBox("请选择开始时间和结束时间", MsgBoxStyle.Information, "提示")
        End If
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If dgv.SelectedRows.Count > 0 Then
            Dim dtGtPgdMesWorkReport As DataTable = objGtPgdMesWorkReport.getListByGtPgdMesSysId(dgv.SelectedRows(0).Cells("sys_id").Value.ToString())
            If dtGtPgdMesWorkReport.Rows.Count = 0 Then
                Dim rtn As Integer = objWorkshopCurrent.SaveData("1", "GJB", dgv.SelectedRows(0).Cells("support_model").Value.ToString(),
                                                                    dgv.SelectedRows(0).Cells("sys_id").Value.ToString(),
                                                                    ok_num, b_gt_sys_id)
                If rtn = -2 Then
                    MsgBox(btnSave.Text + "失败")
                Else
                    MsgBox(btnSave.Text + "成功")
                    CType(Owner, FormMain).initForm()
                End If
            Else
                If MsgBox("该选择的生产计划已进行报工过，确认保存设定吗？", vbYesNo, "提示") = vbYes Then
                    Dim rtn As Integer = objWorkshopCurrent.SaveData("1", "GJBZP", dgv.SelectedRows(0).Cells("support_model").Value.ToString(),
                                                                    dgv.SelectedRows(0).Cells("sys_id").Value.ToString(),
                                                                    ok_num, b_gt_sys_id)
                    If rtn = -2 Then
                        MsgBox(btnSave.Text + "失败")
                    Else
                        MsgBox(btnSave.Text + "成功")
                        CType(Owner, FormMain).initForm()
                    End If
                End If
            End If
        Else
            MsgBox("请选择需设定的生产计划")
        End If
    End Sub
    Private Sub btnMesPlan_Click(sender As Object, e As EventArgs) Handles btnMesPlan.Click
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            'Dim params As String = "{""workshop"":""喷涂"",""date"":""" & "2021-12-08" & """,""compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""}"
            Dim params As String = "{""workshop"":""喷涂"",""startdate"":""" & DateTime.Parse(txtStartDate.Text).ToString("yyyy-MM-dd") & """,""enddate"":""" & DateTime.Parse(txtEndDate.Text).ToString("yyyy-MM-dd") & """,""compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""}"
            Dim rtn As String = httpHelper.HttpPostWebService("findDispatchOrderInfoListByTimeAndCJ", params)
            'MsgBox(rtn)
            Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
            Dim code As String = jo("code").ToString() '输出0
            'MsgBox(code)
            Dim data As String = jo("data").ToString() '输出0
            'data = "[{""dispatchingorder"":""PGDH211103001"",
            '""start_time"":""2021-11-02 00:00:00"",
            '""end_time"":""2021-11-02 23:59:59"",
            '""project_code"":""fgm-test-0001"",
            '""project_name"":""fgm-test-0001"",
            '""equip_id"":""070046,070023,070020,070011,"",
            '""jp_type"":""mes不存在"",
            '""prod_spec_id"":""HDR-0.4-201-035-18"",""count"":9.0},
            '{""dispatchingorder"":""PGDH211103222"",""start_time"":""2021-11-02 00:00:00"",
            '""end_time"":""2021-11-02 23:59:59"",
            '""project_code"":""fgm-test-0001"",
            '""project_name"":""fgm-test-0001"",
            '""equip_id"":""070046,070023,070020,070011,"",
            '""jp_type"":""mes不存在"",
            '""prod_spec_id"":""HDR-0.4-201-035-18"",""count"":9.0}]"
            'MsgBox(data)
            Dim jarray As JArray = jarray.Parse(data)
            If jarray.Count > 0 Then
                For index = 0 To jarray.Count - 1
                    Dim en As New entityPgdMesGt
                    en.pgd_code = jarray(index)("dispatchingorder")
                    en.oper_date = DateTime.Parse(jarray(index)("start_time")).ToShortDateString()
                    en.start_time = jarray(index)("start_time")
                    en.end_time = jarray(index)("start_time")
                    en.support_model = jarray(index)("p_prod_spec_id")
                    en.count = 0
                    en.material_code = jarray(index)("prod_spec_id")
                    Dim dtFlbMst As DataTable = objFlbMst.getAllList(jarray(index)("prod_spec_id"))
                    If dtFlbMst.Rows.Count = 1 Then
                        en.oper_type0 = "1"
                        en.type_value0 = dtFlbMst.Rows(0)("mst_value")
                    Else
                        en.oper_type0 = String.Empty
                        en.type_value0 = jarray(index)("prod_spec_id")
                    End If
                    If String.IsNullOrEmpty(en.oper_type0) Then
                        Dim dtGjbMst As DataTable = objGjbMst.getAllList(jarray(index)("prod_spec_id"))
                        If dtGjbMst.Rows.Count = 1 Then
                            en.oper_type0 = "0"
                            en.type_value0 = dtGjbMst.Rows(0)("mst_value")
                        Else
                            en.oper_type0 = String.Empty
                            en.type_value0 = jarray(index)("prod_spec_id")
                        End If
                    End If

                    en.oper_count0 = jarray(index)("count")
                    en.mes_project_code = jarray(index)("project_code")
                    en.mes_project_name = jarray(index)("project_name")

                    en.validity = "1"
                    en.status = "1"
                    en.origin = "0"
                    en.Ope = usr
                    Dim dtmesdata As DataTable = objPgdMesGt.getList(jarray(index)("dispatchingorder").ToString())
                    If dtmesdata.Rows.Count = 0 Then
                        Dim rtn_save As Integer = objPgdMesGt.insertMes(en)
                    Else
                        Dim rtn_save As Integer = objPgdMesGt.updateMes(en)
                    End If
                Next
            Else
                MsgBox("没有数据", MsgBoxStyle.Information, "提示")
            End If
        Else
            MsgBox("请选择开始时间和结束时间", MsgBoxStyle.Information, "提示")
        End If
    End Sub
    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txtStartDate.MouseClick, txtEndDate.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub

    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub
End Class